﻿using System.Collections.Generic;

namespace RatingAuthor
{
  public class Author
  {
    public bool IsUpdated { get; set; }
    public bool IsNew { get; set; }
    public AuthorUrl Url { get; set; }
    public string Name { get; set; }
    public Dictionary<string, Book> BooksByUrl { get; private set; }

    public string FullUrl
    {
      get { return "http://zhurnal.lib.ru/" + Url.Effective; }
    }

    public Author()
    {
      BooksByUrl = new Dictionary<string, Book>();
    }

    internal Author(BookRecord r)
    {
      IsNew = true;
      Url = r.AuthorUrl;
      Name = r.AuthorName;
      BooksByUrl = new Dictionary<string, Book>();
    }

    internal bool UpdateBook(BookRecord r, GenresLookup genresLookup)
    {
      Book prev;
      if (BooksByUrl.TryGetValue(r.Url, out prev))
      {
        var res = prev.Update(r);
        IsUpdated |= res;
        return res;
      }

      var book = new Book(r, genresLookup);
      BooksByUrl[book.Url] = book;
      book.Author = this;
      IsUpdated = true;
      return false;
    }
  }
}